{extend name="layer" /}
{block name='content'}
    <!-- form start -->
    <form role="form">
        <div class="card">

            <!--<div class="card-header">
                <h3 class="card-title">
                    <i class="fas fa-tag"></i>
                    新增角色
                </h3>
            </div>-->
            <!-- /.card-header -->
            <div class="card-body">
                <div class="form-group">
                    <label for="parent_id">所属菜单</label>
                    <select type="text" class="form-control" id="parent_id" name="parent_id">
                        {$options}
                    </select>
                </div>
                <div class="form-group">
                    <label for="menu_name">菜单名称</label>
                    <input type="hidden" class="form-control" id="menu_id" value="{$data.id}" name="id">
                    <input type="text" class="form-control" id="menu_name" value="{$data.menu_name}" name="menu_name" placeholder="请输入菜单名称">
                </div>
                <div class="form-group">
                    <label for="menu_type">类型</label>
                    <select id="menu_type" name="menu_type" class="form-control">
                        <option>请选择</option>
                        {volist name="types" id="vo" key="k"}
                        {if condition="$data.menu_type==$vo.type"}
                        <option value="{$vo.type}" selected>{$vo.text}</option>
                        {else /}
                        <option value="{$vo.type}">{$vo.text}</option>
                        {/if}
                        {/volist}
                    </select>
                </div>
                <div class="form-group">
                    <label for="icon">图标</label>
                    <input type="text" class="form-control" id="icon" value="{$data.icon}" name="icon" placeholder="请输入菜单图标">
                </div>
                <div class="form-group">
                    <label for="url_value">链接</label>
                    <input type="text" class="form-control" id="url_value" value="{$data.url_value}" name="url_value" placeholder="请输入菜单链接">
                </div>
                <div class="form-group">
                    <label for="url_value">参数</label>
                    <input type="text" class="form-control" id="params" name="params" value="{$data.params}" placeholder="请输入参数">
                </div>
                <div class="form-group">
                    <label for="sort">排序</label>
                    <input type="text" class="form-control" id="sort" name="sort" value="{$data.sort}" placeholder="请输入菜单排序">
                </div>
                <div class="form-group">
                    <label for="">状态</label>
                    <div style="display: flex">
                        <div class="custom-control custom-radio col-3">
                            {if condition="$data['status']==1"}
                            <input class="custom-control-input" type="radio" id="customRadio1" value="1" name="status" checked>
                            {else /}
                            <input class="custom-control-input" type="radio" id="customRadio1" value="1" name="status" >
                            {/if}
                            <label for="customRadio1" class="custom-control-label">开启</label>
                        </div>
                        <div class="custom-control custom-radio col-3">
                            {if condition="$data['status']==1"}
                            <input class="custom-control-input" type="radio" id="customRadio2" value="0" name="status">
                            {else /}
                            <input class="custom-control-input" type="radio" id="customRadio2"  value="0" name="status" checked >
                            {/if}
                            <label for="customRadio2" class="custom-control-label">禁用</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label>角色</label>
                    <select class="select2" name="roles[]" multiple="multiple" data-placeholder="请选择至少一项" style="width: 100%;">

                    </select>
                </div>
            </div>
            <!-- /.card-body -->

            <div class="card-footer">
                <button type="button" id="submit" class="btn btn-primary">保存</button>
            </div>
        </div>
    </form>
{/block}
{block name='pagejs'}
<script>

    var index = parent.layer.getFrameIndex(window.name);

    var select2Json = {$roles};
    $('.select2').select2({
        data: select2Json.list.data
    });
    $(".select2").val(select2Json.selected).trigger('change');

    $("#submit").on('click', function () {
        var formData = new FormData($('form')[0]);
        $.ajax({
            cache: true,
            type: "POST",
            url:"{:url('admin/menu/doEdit')}",
            data:formData,
            dataType:'json',
            processData : false,
            async: true,
            contentType: false,
            error:function(XMLHttpRequest, textStatus, errorThrown){
                // alert(textStatus);
            },
            success: function( response ) {
                var code = parseInt( response.error_code );
                switch (code) {
                    case 1:
                        popupBox( getMsgBox( {'success': response.msg}, 'success' ), function (alert_layero, alert_index) {
                            setTimeout(function () {
                                layer.close( alert_index )
                            }, 600);
                        } );

                        setTimeout(function () {

                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();
                        }, 800);
                        break;
                    case 10000:
                        popupBox( getMsgBox( response.msg, 'danger' ) );
                        break;
                    default:
                        popupBox( getMsgBox( '操作出现问题了', 'danger' ) );
                }
            }
        });
    });
</script>
{/block}
